package com.example.wyc.mybiye1;

import android.app.Activity;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.wyc.mybiye.R;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Ma3Activity{
    Activity mContext;
    Context context_t;
    private LayoutInflater mInflater;
    private View mCurrentView;

    //---------
    public BufferedReader in = null;
    public PrintWriter out = null;
    public Socket socket;
    public ServerSocket serverSocket;
    //--
    public EditText eT_ip,eT_duanKou,eT_shuJu;
    public Button bT_lianJie,bT_faSong;
    public TextView tV_jiLu;
    //--

    public Ma3Activity(Activity context, Context context_t){
        this.mContext=context;
        this.context_t=context_t;
        //为之后将Layout转化为view时用
        mInflater = LayoutInflater.from(mContext);
    }

    public void initView(){
        //设置布局文件
        mCurrentView = mInflater.inflate(R.layout.activity_ma3, null);
        eT_ip=mCurrentView.findViewById(R.id.eT_ip);
        eT_duanKou=mCurrentView.findViewById(R.id.eT_duanKou);
        eT_shuJu=mCurrentView.findViewById(R.id.eT_shuJu);
        //--
        bT_lianJie=mCurrentView.findViewById(R.id.bT_lianJie);
        bT_faSong=mCurrentView.findViewById(R.id.bT_faSong);
        //--
        tV_jiLu=mCurrentView.findViewById(R.id.tV_jiLu);
        tV_jiLu.setMovementMethod(new ScrollingMovementMethod());
        tV_jiLu.append("\n接收信息记录\n");
        //--
        jianTing();
    }
    public String ip;
    public int duanKou;
    public void jianTing(){
        bT_lianJie.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ip= String.valueOf(eT_ip.getText());
                String str= String.valueOf(eT_duanKou.getText());
                duanKou= Integer.parseInt(str);
                thread1.start();
            }
        });
        bT_faSong.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                thread3.start();
                System.out.print("11");
            }
        });
    }

    Thread thread1=new Thread(new Runnable() {
        @Override
        public void run() {
            try {
                socket=new Socket(ip,duanKou);
                in = new BufferedReader(new InputStreamReader(socket.getInputStream(), "UTF-8"));
                out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                Message msg=new Message();
                msg.what=2;
                msg.obj="连接成功！";
                handler.sendMessage(msg);
            } catch (Exception e) {
                Message msg=new Message();
                msg.what=1;
                msg.obj="连接失败XX";
                handler.sendMessage(msg);
                e.printStackTrace();
                return;
            }

        }
    });

    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    Toast.makeText(context_t,msg.obj.toString(),Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(context_t,msg.obj.toString(),Toast.LENGTH_LONG).show();
                    thread2.start();
                    isThread2=true;
                    break;
                case 3:
                    SimpleDateFormat simpleDateFormat=new SimpleDateFormat("HH:mm:ss");
                    String time=simpleDateFormat.format(new Date());
                    tV_jiLu.append("时间："+time+"  内容："+msg.obj.toString()+"\n");
                    break;
            }
        }
    };
    boolean isThread2=false;
    public String xiaoXi;
    Thread thread2=new Thread(new Runnable() {
        @Override
        public void run() {
            while (true){
                yanchi(1000);
                if (isThread2==true){
                    try {
                        xiaoXi=in.readLine();
                        if (xiaoXi !=null){
                            Message msg=new Message();
                            msg.what=3;
                            msg.obj=xiaoXi;
                            handler.sendMessage(msg);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }else if (isThread2==false){

                }
            }
        }
    });

    Thread thread3=new Thread(new Runnable() {
        @Override
        public void run() {
            if (eT_shuJu.getText().toString()!=""){
                out.println(eT_shuJu.getText().toString());
                Message msg=new Message();
                msg.what=1;
                msg.obj="发送成功！";
                handler.sendMessage(msg);
            }else {
                Message msg=new Message();
                msg.what=1;
                msg.obj="发送失败！";
                handler.sendMessage(msg);
            }
        }
    });

    public void yanchi(int i){
        try {
            Thread.sleep(i);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * 获取当前在导航栏上方显示对应的View
     */
    public View getView() {
        if (mCurrentView == null) {
            initView();
        }
        return mCurrentView;
    }
    /**
     * 显示当前导航栏上方所对应的view界面
     */
    public void showView(){
        if(mCurrentView == null){
            initView();
        }
        mCurrentView.setVisibility(View.VISIBLE);
    }
}
